<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta
        http-equiv="Content-Type"
        content="text/html; charset=utf-8"
    />
    <title>8创建角色动画</title>

    <style>
        html,
        body {
            overflow: hidden;
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }

        #renderCanvas {
            width: 100%;
            height: 100%;
            touch-action: none;
        }
    </style>

    <script src="https://cdn.babylonjs.com/babylon.js"></script>
    <!-- 允许您将模型导入到场景中 -->
    <script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
    <!-- 允许您使用触摸屏 -->
    <script src="https://code.jquery.com/pep/0.4.3/pep.js"></script>
    <!-- earcut算法 -->
    <script src="../../src/earcut.js"></script>
</head>

<body>
    <canvas
        id="renderCanvas"
        touch-action="none"
    ></canvas>
    <!-- 注意browseatouch-action="none"以获得PEPr/canvas调整大小事件的最佳效果 -->


    <script>
        const canvas = document.getElementById("renderCanvas"); //获取canvas元素
        const engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine

        //在这里添加与playground格式匹配的代码
        const createScene = function () {
            const scene = new BABYLON.Scene(engine);//使用引擎
            const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 15, new BABYLON.Vector3(0, 0, 0));//设置摄像机
            camera.attachControl(canvas, true);
            const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0));//设置灯光


            BABYLON.SceneLoader.ImportMeshAsync(
                "",
                "../../src/models/",
                "scene.glb",
                scene
            );
            const walk = function (turn, dist) {
                this.turn = turn;
                this.dist = dist;
            };

            const track = [];
            track.push(new walk(86, 7));
            track.push(new walk(-85, 14.8));
            track.push(new walk(-93, 16.5));
            track.push(new walk(48, 25.5));
            track.push(new walk(-112, 30.5));
            track.push(new walk(-72, 33.2));
            track.push(new walk(42, 37.5));
            track.push(new walk(-98, 45.2));
            track.push(new walk(0, 47));


            BABYLON.SceneLoader.ImportMeshAsync(
                "him",
                "../../src/models/dude/",
                "Dude.babylon",
                scene
            ).then((result) => {
                console.log(result);
                var dude = result.meshes[0];
                dude.scaling = new BABYLON.Vector3(0.01, 0.01, 0.01);
                dude.position = new BABYLON.Vector3(-6, 0, 0);
                dude.rotate(
                    BABYLON.Axis.Y,
                    BABYLON.Tools.ToRadians(-95),//将角度换算成弧度
                    BABYLON.Space.Local
                );
                const startRotation = dude.rotationQuaternion.clone();
                scene.beginAnimation(result.skeletons[0], 0, 100, true, 1.0);

                let distance = 0;
                let step = 0.015;
                let p = 0;

                scene.onBeforeRenderObservable.add(() => {
                    dude.movePOV(0, 0, step);
                    distance += step;
                    if (distance > track[p].dist) {
                        dude.rotate(BABYLON.Axis.Y, BABYLON.Tools.ToRadians(track[p].turn), BABYLON.Space.LOCAL);
                        p += 1;
                        p %= track.length;
                        if (p === 0) {
                            distance = 0;
                            dude.position = new BABYLON.Vector3(-6, 0, 0);
                            dude.rotationQuaternion = startRotation.clone();
                        }
                    }
                })
            });

            return scene;

        }
        const scene = createScene(); //调用createScene函数

        //注册一个渲染循环来重复渲染场景
        engine.runRenderLoop(function () {
            scene.render();
        });

        // 注意浏览器、画布大小调整事件
        window.addEventListener("resize", function () {
            engine.resize();
        });
    </script>
</body>

</html>